package com.crm.controller;


import cn.hutool.core.bean.BeanUtil;
import com.crm.base.PageResult;
import com.crm.base.ResponseResult;
import com.crm.base.UserVo;
import com.crm.domain.dto.UserDto;
import com.crm.domain.entity.Role;
import com.crm.domain.entity.User;

import com.crm.mapper.RoleMapper;
import com.crm.mapper.UserManagementMapper;
import com.crm.service.RoleService;
import com.crm.service.UserManagementService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 15890
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/system")
@Api("用户管理")
public class UserManagementController {

    private final UserManagementService userService;

    private final UserManagementMapper userMapper;


    @GetMapping("/user/list")
    @ApiOperation("用户分页查询")
    public PageResult<User> list(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return userService.getPagedUsers(pageNum, pageSize);
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("根据id查询用户")
    public ResponseResult getUserById(@PathVariable Long userId) {
        userMapper.selectById(userId);
        return ResponseResult.success();
    }

    @GetMapping("/user/listselect")
    @ApiOperation("根据部门id查询用户")
    public ResponseResult<List<User>> listselect(@RequestParam("deptId") Long deptId) {
       List<User> userList =  userService.findUsersByDeptId(deptId);
        return ResponseResult.success(userList);
    }


    @PostMapping("/user")
    @ApiOperation("新增用户")
    public ResponseResult<UserVo> addUser(@RequestBody UserDto userDto) {
        UserVo userVo = userService.createUser(userDto);
        return ResponseResult.success(userVo);
    }

    @DeleteMapping("/user/{userId}")
    @ApiOperation("删除用户")
    public ResponseResult deleteUser(@PathVariable Long userId) {
        userMapper.removeById(userId);
        return ResponseResult.success();
    }

    @GetMapping("/config/configKey/sys.user.initPassword")
    public String configKey() {
        String initPassword = "{msg: \"123456\", code: 200}\n" +
                "code\n" +
                ": \n" +
                "200\n" +
                "msg\n" +
                ": \n" +
                "\"123456\"";

        return initPassword;

    }
}
